﻿using System;
using System.Collections.Generic;
using System.Text;
using Acms.Bll;

namespace Acms.DataControls
{
    [System.ComponentModel.Designer(typeof(TopicGroupDataSourceDesigner))]
    [System.Web.UI.ToolboxData("<{0}:TopicGroupDataSource runat=server/>")]
    public class TopicGroupDataSource : GenericDataSource<TopicGroup>
    {
        public enum Fields
        {
            Id,
            SiteId,
            Name,
            Path,
            Notes,
            ConcurencyId
        }

        protected override TopicGroup PopulateData(UpdateAction action, System.Collections.IDictionary keys, System.Collections.IDictionary values, System.Collections.IDictionary oldValues)
        {
            TopicGroup tmp = null;
            object concurencyId = null;

            if (action == UpdateAction.Insert)
            {
                tmp = TopicGroup.CreateInstance();
            }
            else
            {
                int id = GetValue<int>(Fields.Id, keys);
                concurencyId = GetValue<byte[]>(Fields.ConcurencyId, keys);
                tmp = TopicGroup.GetById(id);
            }

            if (tmp != null)
            {
                tmp.CheckConcurent(concurencyId);

                if (RequiresUpdate<string>(Fields.Name, values, oldValues))
                {
                    tmp.Name = GetValue<string>(Fields.Name, values);
                }

                if (RequiresUpdate<string>(Fields.Path, values, oldValues))
                {
                    tmp.Path = GetValue<string>(Fields.Path, values);
                }

                if (RequiresUpdate<string>(Fields.Notes, values, oldValues))
                {
                    tmp.Notes = GetValue<string>(Fields.Notes, values);
                }
            }

            return tmp;

        }

        protected override int SaveChanges(TopicGroup item)
        {
            item.Update();
            return 1;
        }

        protected override void AddInsertedKey(TopicGroup item, System.Collections.IDictionary values)
        {
            values[Fields.Id.ToString()] = item.Id;
        }

        protected override int Delete(TopicGroup item)
        {
            item.Delete();
            return 1;
        }

        public override bool CanInsert
        {
            get
            {
                return true;
            }
        }

        public override bool CanDelete
        {
            get
            {
                return true;
            }
        }

        public override bool CanUpdate
        {
            get
            {
                return true;
            }
        }
    }

    public class TopicGroupDataSourceDesigner : GenericDataSourceDesigner<TopicGroup>
    {
        protected override TopicGroup GetDesignTimeItem(int index)
        {
            return TopicGroup.CreateInstance();
        }
    }
}
